package com.hooda.server;

import com.hooda.container.Connector;
import com.hooda.container.Engine;
import com.hooda.container.Service;
import com.hooda.servlet.HttpServlet;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;
import java.util.concurrent.*;

public class Bootstrap {

    private int shutdownPort = 8005;

    private ServerSocket shutdownSocket = null;

    private List<Service> services = new ArrayList<>();

    public void run() throws Exception {

        load();

        init();

        start();

        shutdownSocket = new ServerSocket(shutdownPort);
        shutdownSocket.accept();

        destroy();
    }

    private void load() {
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("server.xml");
        SAXReader saxReader = new SAXReader();
        try {
            Document document = saxReader.read(inputStream);
            Element rootElement = document.getRootElement();

            /** 解析Service */
            List<Element> serviceElements = rootElement.selectNodes("//Service");
            if (Objects.isNull(serviceElements)) {
                return;
            }
            for (Element serviceElement : serviceElements) {
                Service service = new Service();

                /** 解析 Connector组件 */
                List<Element> connectorList = serviceElement.selectNodes("//Connector");
                if (Objects.isNull(connectorList)) {
                    continue;
                }
                for (Element element : connectorList) {
                    String port = element.attributeValue("port");
                    if (Objects.isNull(port)) {
                        continue;
                    }
                    service.addConnector(new Connector(Integer.parseInt(port), service));
                }


                /** 解析 Engine组件 */
                Element engineElement = (Element) serviceElement.selectSingleNode("//Engine");
                if (Objects.isNull(engineElement)) {
                    continue;
                }
                List<Element> hostList = engineElement.selectNodes("//Host");
                if (Objects.isNull(hostList)) {
                    continue;
                }
                Engine engine = new Engine();
                for (Element element : hostList) {
                    String name = element.attributeValue("name");
                    String appBase = element.attributeValue("appBase");

                    engine.addHost(name, appBase);
                }
                service.setEngine(engine);

                services.add(service);
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    private void init() {
        for (Service service : services) {
            service.init();
        }
    }

    private void start() {
        for (Service service : services) {
            service.start();
        }
    }

    private void destroy() {
        for (Service service : services) {
            service.destroy();
        }
    }

    public static void main(String[] args) {
        Bootstrap bootstrap = new Bootstrap();
        try {
            bootstrap.run();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
